// package_02/pages/ios-form-bug/index.js
const app = getApp()
const DEGUB = app.globalData.DEGUB
const currentYear = new Date().getFullYear() - 100
var moment = require('moment');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 状态栏的高度
    statusBarHeight: 44,
    // main高度
    mainHeight: 0,
    submitBtnHeight: 0,

    item: {
      id: 3,
      name: 'SK-II 护肤面膜',
      expires_time: '2024-02-26',
      ingredientList: [
        {id: 1, name: '二裂酵母发酵产物溶胞产物', function: '烟酰胺是一种美白成分，它具有抗糖化作用，能淡化蛋白质烟酰胺是一种美白成分，它具有抗糖化作用，能淡化蛋白质糖…'},
        {id: 2, name: '生育酚（维生素E）', function: '抗氧化剂、抗衰老'},
        {id: 3, name: '柠檬酸', function: '防腐剂、去角质'},
        {id: 4, name: '木瓜蛋白酶', function: '抗衰老、去角质'},
        {id: 5, name: '甘油', function: '溶剂、保湿剂'},
        {id: 6, name: '辛甘醇', function: '柔润剂、保湿剂'},
        {id: 7, name: '菠萝果（ANANAS SATIVUS）提菠萝果（ANANAS SATIVUS）提取', function: '去角质、皮肤调理剂'},
        {id: 8, name: '1,3-丙二醇', function: '溶剂、保湿剂'},
        {id: 9, name: '木瓜蛋白酶', function: '抗衰老、去角质'},
        {id: 10, name: '甘油', function: '溶剂、保湿剂'},
        {id: 11, name: '辛甘醇', function: '柔润剂、保湿剂'},
        {id: 12, name: '菠萝果（ANANAS SATIVUS）提菠萝果（ANANAS SATIVUS）提取', function: '去角质、皮肤调理剂'},
        {id: 13, name: '1,3-丙二醇', function: '溶剂、保湿剂'},
        {id: 14, name: '1,3-丙二醇', function: '溶剂、保湿剂'},
        {id: 15, name: '1,3-丙二醇', function: '溶剂、保湿剂'},
        {id: 16, name: '1,3-丙二醇', function: '溶剂、保湿剂'},
        {id: 17, name: '1,3-丙二醇', function: '溶剂、保湿剂'},
      ]
    },

    showPickerH: 0,
    currentDate: new Date().getTime(), //时间选择器的初始值

    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },

    expiresTime: '',

    isLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取设备信息
    const { statusBarHeight } = app.globalData.systemInfo
    this.setData({
      statusBarHeight,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getRect()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },


  // 返回首页
  backPrePage(){
    wx.navigateBack()
  },

  //计算高度
  getRect() {
    var that = this;
　　//我这里需要获取多个元素的高度，所以用的是selectAll
    wx.createSelectorQuery().selectAll('.c-nav-bar, .submit-wrap').boundingClientRect().exec(function(res){
      // DEGUB && console.log(res);
      const { screenHeight, safeArea } = app.globalData.systemInfo
      const cNavBarHeight = res[0][0].height
      const submitBtnHeight = res[0][1].height
      const safeBottom = screenHeight - safeArea.bottom
      const mainHeight = screenHeight - cNavBarHeight - safeBottom
      // const mainHeight = screenHeight - cNavBarHeight - submitBtnHeight - safeBottom
      that.setData({
        mainHeight,
        submitBtnHeight
      })
    })
  },

  /**
   * 产品名称修改
   */
  inputChange(e){
    clearTimeout(this._inputDebounce)
    this._inputDebounce = setTimeout(() => {
      this.setData({
        [`item.name`]: e.detail.value
      })
    }, 800)
  },

  // 弹出picker
  showPicker(){
    this.setData({
      showPickerH: 589
    })
  },

  // 确认--获取出生日期
  getDate(event) {
    this.setData({
      expiresTime: moment(event.detail).format('YYYY-MM-DD').split('-').join('.'),
      showPickerH: 0
    })
  },

  // 取消--picker选择器隐藏
  onCancel(){
    this.setData({
      showPickerH: 0
    })
  },

  /**表单提交 */
  formSubmit(){
    if (this.data.item.name === '') {
      wx.showToast({
        title: '产品名称不能为空',
        icon: 'none'
      })
    }else if (this.data.expiresTime === '') {
      wx.showToast({
        title: '有效日期不能为空',
        icon: 'none'
      })
    }else{
      //提及至后台保存
      console.log(this.data.expiresTime);
      const data = { validityTime: this.data.expiresTime.split('.').join('-'), ...this.data.item }
      console.log(data);
      return
      addSkincareProductItemApi(wx.getStorageSync('token'), data).then(res => {
        console.log('添加护肤品', res);
        if (res.data.code === 200) {
          wx.showToast({
            title: '保存成功',
            mask: true,
            success: () => {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 2
                })
              }, 1000)
            }
          })
        }else if (res.data.code === 401) {
          app.login(wx.getStorageSync('phone'))
        }else if (res.data.code === 500) {
          wx.showToast({
            title: res.data.msg,
            mask: true,
            icon: 'none'
          })
        }
      })
    }
  }
  
})